package com.qtest.qandroid.controller;

import com.qtest.qandroid.entity.AttendanceJob;
import com.qtest.qandroid.entity.Homework;
import com.qtest.qandroid.entity.Result;
import com.qtest.qandroid.service.HomeworkService;
import com.qtest.qandroid.util.timecha;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class HomeworkController {
    @Autowired
    private HomeworkService homeworkService;

   @RequestMapping(value = "/addHomework",method = RequestMethod.POST)
    public Result<String> addHomework(@RequestBody Homework homework){
       Result<String> result = new Result<>();
       timecha.change();
       if (homeworkService.addHomework(homework)){
           result.setNum("1");
           result.setMessage("添加成功");
           return result;
       }
       else{
           result.setNum("0");
           result.setMessage("添加失败");
           return result;
       }
   }

   @RequestMapping(value = "/allHomework",method = RequestMethod.POST)
    public Result<List<Homework>> allHomework(@RequestBody JSONObject s){
       String courseId = s.getString("courseId");
       Result<List<Homework>> result = new Result<>();
       List<Homework> homework = homeworkService.allHomework(courseId);
       if (homework.size()<=0){
           result.setNum("0");
           result.setMessage("课程下无作业");
           return result;
       }else {
           result.setNum("1");
           result.setMessage("作业信息");
           result.setData(homework);
           return result;
       }
   }

}
